package be.kul.osgi.http.fileserver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.eclipse.core.runtime.FileLocator;
import org.osgi.framework.BundleContext;


public class Server{
	
	String contentpath;
	
	public void activate(BundleContext c){
		//when activating server, set the location for the web-content
		try {
			contentpath = FileLocator.getBundleFile(c.getBundle()).getAbsolutePath() + File.separator +"web-content";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        System.out.println(contentpath);
	}
	
	public String publish(String path, String filename) throws Exception
	{
		System.out.println("@publishing path to zip: " + path);
		FileInputStream fis  = new FileInputStream(path + File.separator +filename);
	    FileOutputStream fos = new FileOutputStream(this.contentpath + File.separator +  filename);
	    System.out.println("@publishing path webcontent: " + this.contentpath);
		try {
	        byte[] buf = new byte[1024];
	        int i = 0;
	        while ((i = fis.read(buf)) != -1) {
	            fos.write(buf, 0, i);
	        }
	    } 
	    catch (Exception e) {
	        throw e;
	    }
	    finally {
	        if (fis != null) fis.close();
	        if (fos != null) fos.close();
	    }	
		return filename;
	}
}
